package com.easylinkin.linkappapi.lobar.controller.app.enterprise;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.dto.UserCountByProjectDTO;
import com.easylinkin.linkappapi.lobar.dto.UserCountByWorkDTO;
import com.easylinkin.linkappapi.lobar.dto.UserProjectDTO;
import com.easylinkin.linkappapi.lobar.dto.app.EnterpriseDTO;
import com.easylinkin.linkappapi.lobar.dto.app.EpidemicDTO;
import com.easylinkin.linkappapi.lobar.dto.app.LobarUserDTO;
import com.easylinkin.linkappapi.lobar.dto.screen.LobarScreenDTO;
import com.easylinkin.linkappapi.lobar.service.LobarScreenService;
import com.easylinkin.linkappapi.lobar.service.UserProjectService;
import com.easylinkin.linkappapi.tenant.dto.LinkappTenantDTO;
import io.swagger.annotations.Api;
import java.util.*;
import java.util.stream.Collectors;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: app端企业级花名册
 */
@RestController
@RequestMapping("enterpriseUser")
@Api(tags = "app端企业级花名册")
public class EnterpriseUserProjectController {
  @Autowired
  private UserProjectService userProjectService;
  @Autowired
  private LobarScreenService lobarScreenService;

  /**
   * 实时人员统计（出勤/在场）/app企业级
   */
  @GetMapping("omNum")
  public RestMessage getOmNumByTid(String tenantId){
    List<LobarUserDTO> lobarUserDTOS = userProjectService.getOmNumByTid(tenantId);
    return RestBuilders.successBuilder().data(lobarUserDTOS).build();
  }

  /**
   * 人员分布分析
   */
  @GetMapping("countUser")
  public RestMessage countUser(String tenantId, @RequestParam(value = "companyId",required = false) String companyId) {
    Map<String, List<LobarScreenDTO>> map = lobarScreenService.countAllUser(tenantId,companyId);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 人员分布分析-按年龄段
   */
  @GetMapping("listGroupByAgePeriod")
  public RestMessage listGroupByAgePeriod(@RequestParam(value = "tenantId", required = false) String tenantId, @RequestParam(value = "companyId", required = false) String companyId) {
    return RestBuilders.successBuilder().data(lobarScreenService.listGroupByAgePeriod(tenantId, companyId)).build();
  }

  /**
   * 项目人员数列表
   */
  @GetMapping("listUserCountByProject")
  @ApiOperation(value = "项目人员数列表")
  public RestMessage listUserCountByProject(@RequestParam(value = "companyId", required = false) String companyId) {
    List<UserCountByProjectDTO> list = lobarScreenService.listUserCountByProject(companyId);
    return RestBuilders.successBuilder().data(list).build();
  }

  /**
   * 人员数按工种分布
   */
  @GetMapping("listUserCountByWork")
  @ApiOperation(value = "人员数按工种分布")
  public RestMessage listUserCountByWork(@RequestParam(value = "companyId", required = false) String companyId) {
    List<UserCountByWorkDTO> list = lobarScreenService.listUserCountByWork(companyId);
    return RestBuilders.successBuilder().data(list).build();
  }

  /**
   * 作业人员统计
   */
  @GetMapping("workType")
  public RestMessage countWorkType(String tenantId){
    //作业人员统计
    Map<String, List<LobarScreenDTO>> map = lobarScreenService.countWorkType(tenantId);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 项目详情
   */
  @PostMapping("project")
  public RestMessage countProject(@RequestBody RequestModel<UserProjectDTO> requestModel2){
    IPage<LinkappTenantDTO> record = lobarScreenService.countProject(requestModel2);
    List<String> tenantIds = record.getRecords().stream().map(LinkappTenantDTO::getId).collect(Collectors.toList());

    List<LobarUserDTO> list = new ArrayList<>();
    RequestModel<UserProjectDTO> requestModel = new RequestModel<>();
    UserProjectDTO userProjectDTO = new UserProjectDTO();
    userProjectDTO.setTenantIds(tenantIds);
    userProjectDTO.setStartTime(DateUtil.beginOfDay(new Date()));
    requestModel.setCustomQueryParams(userProjectDTO);
    requestModel.setPage(new Page(0,-1));
    IPage<LinkappTenantDTO> iPage = lobarScreenService.countProject(requestModel);
    List<LobarUserDTO> dtoList = iPage.getRecords().stream()
            .flatMap(l -> l.getLobarUserDTOS().stream()).collect(Collectors.toList());
    getUsers(list, dtoList,"项目部管理人员");
    getUsers(list, dtoList,"其他管理人员");
    getUsers(list, dtoList,"现场作业人员");
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * 当前现场人员统计/app企业级
   */
  @GetMapping("projectUser")
  public RestMessage countAll(String tenantId){
    List<LobarUserDTO> list = new ArrayList<>();
    RequestModel<UserProjectDTO> requestModel = new RequestModel<>();
    UserProjectDTO userProjectDTO = new UserProjectDTO();
    userProjectDTO.setTenantId(tenantId);
    userProjectDTO.setStartTime(DateUtil.beginOfDay(new Date()));
    requestModel.setCustomQueryParams(userProjectDTO);
    requestModel.setPage(new Page(0,-1));

    IPage<LinkappTenantDTO> iPage = lobarScreenService.countProject(requestModel);
    List<LobarUserDTO> dtoList = iPage.getRecords().stream()
        .flatMap(l -> l.getLobarUserDTOS().stream()).collect(Collectors.toList());
    getUsers(list, dtoList,"项目部管理人员");
    getUsers(list, dtoList,"其他管理人员");
    getUsers(list, dtoList,"现场作业人员");
    return RestBuilders.successBuilder().data(list).build();
  }

  /**
   * 当日疫情分析
   */
  @GetMapping("epidemic")
  public RestMessage countEpidemic(String tenantId){
    List<Map<String, Object>> list = lobarScreenService.countEpidemic(tenantId);
    return RestBuilders.successBuilder().data(list).build();
  }

  /**
   * 防疫统计 app
   */
  @GetMapping("app/epidemic")
  public RestMessage countAppEpidemic(){
    Map<String, Object> map = lobarScreenService.countAppEpidemic();
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 防疫统计详情 app端
   */
  @PostMapping("app/epidemic/detail")
  public RestMessage countAppEpidemicDetail(@RequestBody RequestModel<UserProjectDTO> requestModel){
    IPage<EpidemicDTO> record = lobarScreenService.countAppEpidemicDetail(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * 参建单位分析
   */
  @GetMapping("company")
  public RestMessage countCompany(String tenantId){
    List<Map<String, Object>> list = lobarScreenService.countCompany(tenantId);
    return RestBuilders.successBuilder().data(list).build();
  }

  /**
   * 参建单位分析 app端
   */
  @GetMapping("app/company")
  public RestMessage countAppCompany(){
    Map<String, Integer> map = lobarScreenService.countAppCompany();
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * 参建单位分析详情 app端
   */
  @PostMapping("app/company/detail")
  public RestMessage countAppCompanyDetail(@RequestBody RequestModel<UserProjectDTO> requestModel){
    IPage<EnterpriseDTO> record = lobarScreenService.countAppCompanyDetail(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * 在场情况
   */
  @GetMapping("onArea")
  public RestMessage onArea(){
    Map<String, Integer> map = lobarScreenService.onArea();
    return RestBuilders.successBuilder().data(map).build();
  }




  public static void getUsers(List<LobarUserDTO> list, List<LobarUserDTO> dtoList, String name) {
    List<LobarUserDTO> userDTOS = dtoList.stream().filter(d -> name.equals(d.getName()))
        .collect(Collectors.toList());
    LobarUserDTO lobarUserDTO = new LobarUserDTO();
    lobarUserDTO.setName(userDTOS.get(0).getName());
    int in = userDTOS.stream().mapToInt(u -> u.getIn()).sum();
    int on = userDTOS.stream().mapToInt(u -> u.getOn()).sum();
    int attendance = userDTOS.stream().mapToInt(u -> u.getAttendance()).sum();
    lobarUserDTO.setIn(in);
    lobarUserDTO.setOn(on);
    lobarUserDTO.setAttendance(attendance);
    list.add(lobarUserDTO);
  }
}
